package cn.kgc.web.controller;

import cn.hutool.core.util.IdUtil;
import cn.kgc.base.entity.Clazz;
import cn.kgc.base.utils.ResponseResult;
import cn.kgc.web.service.ClazzService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

/**
 * @author 课工场
 * @date 2024/1/11
 * @description
 */
@RestController
@RequestMapping("clazz")
public class ClazzController {


    @Resource
    private ClazzService clazzService;
    // 班级列表  tagId  tag
    @GetMapping
    public ResponseResult clazzList(){
        return clazzService.clazzList();
    }

    // 删除班级信息
    @DeleteMapping("{id}")
    public ResponseResult removeClazz(@PathVariable("id") Integer id){
        clazzService.removeById(id);
        return ResponseResult.success().message("删除成功");
    }


    // 添加班级信息
    @PostMapping
    public ResponseResult removeClazz(MultipartFile logo,String name, Integer tagId){

        //1. 文件上传
        String originalFilename = logo.getOriginalFilename();
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
        String uuid = IdUtil.simpleUUID();

        String lastName = uuid+"."+filenameExtension;

        File file = new File("D:\\upload\\" + lastName);
        try {

            logo.transferTo(file);
            // 将班级信息写入数据库
            Clazz clazz = new Clazz();
            clazz.setName(name);
            clazz.setTagid(tagId);
            clazz.setPath(lastName);
            clazzService.save(clazz);
            return ResponseResult.success().message("添加成功").data(clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    @GetMapping("{id}")
    public Clazz getById(@PathVariable Integer  id){
        return clazzService.getById(id);
    }



}
